home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / editor / auror300.zip / GLOBREPL.AML < prev    next >
Text File  |  1996-07-17  |  14KB  |  518 lines

  1. //--------------------------------------------------------------------
  2. // GLOBREPL.AML
  3. // Global Replace, (C) 1993-1996 by nuText Systems
  4. //
  5. // (See Globrepl.dox for user help)
  6. //
  7. // This macro performs a global search and replace in multiple files on
  8. // disk. The following search and replace options can be specified:
  9. //
  10. //   a - replace all occurrences of the search string without prompting.
  11. //       (if this option is not specified, a verification window will be
  12. //       displayed, you will be prompted to verify each replacement).
  13. //   B - backup files before they are modified
  14. //   i - ignore case
  15. //   u - search within subdirectories also
  16. //   w - whole words only
  17. //   x - regular expressions
  18. //
  19. // If this macro is run from a file manager window, then only the files
  20. // listed in the file manager are searched, otherwise you will be
  21. // prompted to enter a directory or file specification where files will
  22. // be searched.
  23. //
  24. // After successful completion of this macro, a summary window is
  25. // displayed, showing the total number of replacements and the number
  26. // of replacements for each file.
  27. //
  28. // Usage:
  29. //
  30. // Select this macro from the Macro List (on the Macro menu), or run it
  31. // from the macro picklist <shift f12>.
  32. //--------------------------------------------------------------------
  33.  
  34. include bootpath "define.aml"
  35.  
  36. // status window colors
  37. constant status_border_color      = color black on gray
  38. constant status_border_color_all  = color white on gray   // replace-all
  39. constant status_text_color        = color black on gray
  40. constant status_count_color       = color red   on gray
  41. constant status_marker_color      = color brightgreen on gray
  42.  
  43. // status window dimensions for replace-all
  44. constant status_qwidth  = 70  // (for fully qualified scans only)
  45. constant status_height  = 19
  46.  
  47. // status window height for replace-verify
  48. // (as a percentage of the screen)
  49. constant status_size    = 33
  50.  
  51. // use fmgr files if run from the file manager
  52. if wintype? "fmgr" then
  53.   passedbuf = getcurrbuf
  54. end
  55.  
  56. // inherit from the 'win' object
  57. settype "win"
  58.  
  59. variable statuswin, statusbottom, verifywin
  60.  
  61. // create the status window
  62. private function createstatus (width)
  63.   statuswin = createwindow
  64.   setshadow 2 1
  65.   settitle "Global Replace" 'c'
  66.  
  67.   setcolor  border_color
  68.      (if? width status_border_color_all status_border_color)
  69.   setcolor  north_title_color  status_border_color
  70.   setcolor  text_color         status_text_color
  71.  
  72.   // replace all
  73.   if width then
  74.     setframe ">sb"
  75.     setcolor border_color status_border_color_all
  76.     setcolor south_title_color  status_border_color
  77.     setborder "1i"
  78.     // center the window
  79.     ox = (getvidcols - width) / 2
  80.     oy = (getvidrows - status_height) / 2
  81.     sizewindow ox oy ox + width oy + status_height "ad"
  82.  
  83.   // replace-verify
  84.   else
  85.     setframe "><b"
  86.     setcolor border_color status_border_color
  87.     setborder "1"
  88.     statusbottom = (getvidrows * status_size) / 100
  89.     sizewindow 0 0 getvidcols - 1 statusbottom "ad"
  90.   end
  91. end
  92.  
  93. // create the verify window
  94. private function createverify
  95.   verifywin = createwindow
  96.   setcolor border_color status_text_color
  97.   setcolor text_color        (getpalette 2)
  98.   setcolor menu_color        (getpalette 18)
  99.   setcolor menu_hotkey_color (getpalette 20)
  100.   setcolor scroll_color      (getpalette 22)
  101.   setcolor control_color     (getpalette 17)
  102.   setcolor endoftext_color   (getpalette 7)
  103.   setborder "1"
  104.   setframe '><bv4'
  105.   setwinctrl '≡'
  106.   eotstring (if? _EOTLine _EOTLine -1)
  107.   menubar '' 4
  108.     item "Replace?"
  109.     item "{Enter,Y}=Yes"
  110.     item "{N}=No"
  111.     item "{A}=All"
  112.     item "{O}=One"
  113.     item "{U}=Undo"
  114.     item "{Q}=Quit"
  115.     item "{Esc}=Quit All"
  116.   end
  117.   sizewindow 0 statusbottom + 1 getvidcols - 1 getvidrows - 1 'ad'
  118. end
  119.  
  120. // edit fields/groupbox windows
  121. variable f1, f2, f3, g1, g2
  122.  
  123. // retrieve history in dlgbox fields and options
  124. function updatehist
  125.   variable searchstr, replstr, options
  126.   n = splitstr '' (gettext) ref searchstr ref replstr ref options
  127.   if n == 2 then
  128.     options = replstr
  129.     replstr = ''
  130.   end
  131.  
  132.   // search string
  133.   buffer = getwinbuf f1
  134.   delchar MAX_COL 1 '' buffer
  135.   instext searchstr 1 '' buffer
  136.  
  137.   // replace string
  138.   if f2 then
  139.     buffer = getwinbuf f2
  140.     delchar MAX_COL 1 '' buffer
  141.     instext replstr 1 '' buffer
  142.   end
  143.  
  144.   // default options
  145.   if not options and n <= 1 then
  146.     options = _SearchOpt + _ReplaceOpt
  147.   end
  148.  
  149.   setgroupbox options "iwx"  g1
  150.   setgroupbox options "aBu"  g2
  151.   col getlinelen + 1
  152. end
  153.  
  154.  
  155. macrofile = arg 1
  156.  
  157. // called by Lib.x when a key is entered in the dialog box
  158. function ondialog (keycode)
  159.   // macro help
  160.   if keycode == <f1> then
  161.     helpmacro macrofile
  162.   end
  163. end
  164.  
  165.  
  166. variable searchstr, replstr, filespec, options, options2
  167.  
  168. // global replace prompt
  169. private function askgrepl
  170.   //if passedbuf or _PromptStyle == 'd' then
  171.     file = getbufname
  172.     dlg = dialog "Global Search and Replace" 66 11 'cp'
  173.     f1 = field "&Search for:   >" 3 2  36 '' "_find"  whenselect "updatehist"
  174.     f2 = field "&Replace with: >" 3 4  36 '' "_find"  whenselect "updatehist"
  175.     if passedbuf then
  176.       filespec = getbufname passedbuf
  177.       gotowindow dlg
  178.       writestr "In Files:     " + (onname filespec) '' 3 6
  179.       gotowindow f2
  180.     else
  181.       f3 = field "In &Files:     >" 3 6  36
  182.                  (onname (qualify "*.*" file)) "_load"
  183.     end
  184.  
  185.     g1 = groupbox '' 3 8
  186.       (menu ''
  187.          item " [ ] &Ignore Case"
  188.          item " [ ] &Whole Words"
  189.          item " [ ] Regular E&Xpression "
  190.        end ) '' _SearchOpt 'iwx'
  191.  
  192.     g2 = groupbox '' 29 8
  193.       (if passedbuf then
  194.          menu ''
  195.            item " [ ] Replace &All        "
  196.            item " [ ] &Backup Files"
  197.          end
  198.        else
  199.          menu ''
  200.            item " [ ] Replace &All"
  201.            item " [ ] &Backup Files"
  202.            item " [ ] S&Ubdirectories     "
  203.          end
  204.        end) '' _SearchOpt 'aBu'
  205.  
  206.     button "O&k"    56 2 8
  207.     button "Cancel" 56 4 8
  208.     value = if passedbuf then
  209.               getdialog ref searchstr ref replstr ref options ref options2
  210.             else
  211.               getdialog ref searchstr ref replstr ref filespec ref options ref options2
  212.             end
  213.     if value == 'Ok' then
  214.       joinstr '' searchstr replstr filespec (options + options2)
  215.     end
  216.   //else
  217.     //ask "[string/replstr/files/iwxasu] Global Replace" "_find"
  218.   //end
  219. end
  220.  
  221.  
  222. variable keycode
  223.  
  224. // search and replace with verification
  225. // (returns the number of replacements made)
  226. private function verify (searchstr replstr options)
  227.  
  228.   variable len, badkey
  229.  
  230.   keycode = 0
  231.   repeat
  232.  
  233.     if not badkey then
  234.       len = find searchstr options + (if? len '' '*')
  235.     end
  236.  
  237.     if len then
  238.  
  239.       // adjust the view
  240.       edit.onfound len
  241.  
  242.       // get keycode
  243.       keycode = getkey
  244.       badkey = FALSE
  245.       case keycode
  246.  
  247.         // replace
  248.         when <enter>, <y>, <o>, <a>, <Y>, <O>, <A>
  249.           if keycode == <enter> then
  250.             keycode = <y>
  251.           else
  252.             keycode = keycode | 0x20
  253.           end
  254.           l = (replace searchstr replstr options + "*") - 1
  255.           right l
  256.           if (pos 'x' options) and getcol == getlinelen then
  257.             right
  258.           end
  259.           count = count + 1
  260.           if keycode <> <y> then
  261.             len = ''
  262.             if keycode == <a> then
  263.               count = count + (replace searchstr replstr options + 'a')
  264.             end
  265.           end
  266.  
  267.         // undo
  268.         when <u>, <U>
  269.           if count then
  270.             undo
  271.             count = count - 1
  272.             if getcol == 1 then
  273.               if up then
  274.                 col MAX_COL
  275.               end
  276.             else
  277.               left l
  278.             end
  279.           end
  280.  
  281.         // don't replace
  282.         when <n>, <N>
  283.           // do nothing
  284.  
  285.         // quit
  286.         when <q>, <Q>, <esc>
  287.           count = 0
  288.           break
  289.  
  290.         // unrecognized key
  291.         otherwise
  292.           beep 220 60
  293.           badkey = TRUE
  294.       end
  295.     end
  296.   u